package com.lc.servlet;

import com.lc.service.UserinfoService;
import com.lc.service.impl.UserinfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @description:
 * @author: ManolinCoder
 * @time: 2021/8/20
 */
@WebServlet("/login")
public class loginServlet extends HttpServlet {
    UserinfoService userinfoService = new UserinfoServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收用户输入的值
        String uname =  req.getParameter("username");
        String pwd =  req.getParameter("password");
        if(userinfoService.getUserinfoByUname(uname) != null){
            //成功登录
            HttpSession session = req.getSession();
            session.setAttribute("uname",uname);
            session.setAttribute("pwd",pwd);
            req.getRequestDispatcher("/message.jsp").forward(req,resp);
        }else {
            //登录失败
            req.getRequestDispatcher("/index.jsp").forward(req,resp);
        }
    }
}
